package net.f3322.lingwl13.interceptor;

import net.f3322.lingwl13.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ResourcesInterceptor extends HandlerInterceptorAdapter {
    private List<String> ignoreUrl;
    public ResourcesInterceptor(List<String> ignoreUrl) {
        this.ignoreUrl = ignoreUrl;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        String uri = request.getRequestURI();
        if(user != null){
            if("ADMIN".equals(user.getRole())){
                return true;
            }else{
                for(String url : ignoreUrl){
                    if(uri.indexOf(url) >= 0){
                        return true;
                    }
                }
            }
        }
        if(uri.indexOf("login") >= 0){
            return true;
        }
        request.setAttribute("msg","未登录至云借阅图书管理系统");
        request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        return false;
    }

}
